﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Controllers;
using Hisun.Models;
using Hisun.UserControl;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.DataExchange
{
    public partial class MeterialInputCheck : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert;


        protected void Page_Load(object sender, EventArgs e)
        {

        }




        #region  绑定数据，根据查询条件的类型、内容及页码执行查询。
        private void MyDataBind(int PageIndex)
        {
            int skipAccount = PageIndex * 10;


            List<TABC_CW_Meterial> Query1 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query2 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query3 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query4 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query5 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query6 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query7 = new List<TABC_CW_Meterial>();

            string MyShop = TextBox_WorkShop.Text;
            string MyPId = TextBox_ProductID.Text;
            string MyMId = TextBox_MeterialID.Text;
            string MyBId = TextBox_BillNumber.Text;
            string MyCenterName = TextBox_CenterName.Text;

            if ( MyShop != "")
            {
                Query1 = (from a in db.TABC_CW_Meterial
                         where a.部门.Contains(MyShop)
                         orderby a.产品ID,a.材料编码,a.领用人,a.出库日期
                         select a).ToList();
            }
            else
            { 
                Query1 =  (from a in db.TABC_CW_Meterial
                         orderby a.产品ID,a.材料编码,a.领用人,a.出库日期
                         select a).ToList();
            }
            
            

        
            if( MyPId != "")
            {
                Query2 = (from a in Query1
                          where a.产品ID.StartsWith(MyPId)
                         select a).ToList();
            }
            else
            {
                Query2 = (from a in  Query1
                             select  a).ToList();
                         
            }


            if (MyMId != "")
            {
                Query3 = (from a in Query2
                          where a.材料编码.StartsWith(MyMId)
                          select a).ToList();
            }
            else
            {
                Query3 = (from a in Query2
                          select a).ToList();
                         
            }

            if (MyBId != "")
            {
                Query4 = (from a in Query3
                          where (a.领料单号.ToString()).StartsWith(MyBId)
                          select a).ToList();
            }
            else
            {
                Query4 = (from a in Query3
                          select a).ToList();
            }


            if (CheckBox_OnlyErrorRow.Checked == true)
            {
                Query5 = (from a in Query4
                          where a.数据检查 == false
                          select a).ToList();
            }
            else
            {
                Query5 = (from a in Query4
                          where a.数据检查 == true
                          select a).ToList();
            }


            if (CheckBox_Zero.Checked == true)
            {
                Query6 = (from a in Query5
                          where a.金额 == 0
                          select a).ToList();
            }
            else
            {
                Query6 = (from a in Query5
                          select a).ToList();
            }

            if (MyCenterName != "")
            {
                Query7 = (from a in Query6
                          where a.领用人.Contains(MyCenterName)
                          select a).ToList();
            }
            else
            {
                Query7 = (from a in Query6
                          select a).ToList();

            }


//查出当前领料记录对应的作业中心
            var Query8 = (from a in Query7
                         join b in db.TABC_DM_CostCenter_Ref_Username
                         on a.领用人 equals b.领用人  
                         into temps
                         from temp in temps.DefaultIfEmpty()
                         select new
                         {
                             a.ID,
                             a.部门,
                             a.材料编码,
                             a.领料单号,
                             a.材料名称,
                             a.产品ID,
                             a.产品名称,
                             a.出库日期,
                             a.计量单位,
                             a.数量,
                             a.金额,
                             a.领用人,
                             作业中心ID =(temp == null ? 0:temp.作业中心ID) ,
                             中间体ID = (temp == null ? "":(temp.作业中心ID+"-"+a.产品ID).ToString())

                         }).ToList();


//查询出当前材料记录对应的中间体信息。
            var Query9 = (from a in Query8
                          join b in db.TABC_JS_ProductInProcess
                          on a.中间体ID equals b.中间体ID into temps 
                          from temp in temps.DefaultIfEmpty()
                          select new
                          {
                              ID =a.ID,
                              部门= a.部门,
                              材料编码=a.材料编码,
                              材料名称=a.材料名称,
                              产品ID=a.产品ID,
                              产品名称=a.产品名称,
                              出库日期=a.出库日期,
                              计量单位=a.计量单位,
                              金额=a.金额,
                              数量=a.数量,
                              领料单号=a.领料单号,
                              领用人=a.领用人,
                              中间体 = (temp == null ? "<font color=red>无</font>" : "有"),
                              可用 = (temp == null || temp.可用 == false ? "<font color=red>否</font>" : "是")



                          }).ToList();


            GridView_MeterialQuery.DataSource = Query9.Skip(skipAccount).Take(10);
            GridView_MeterialQuery.DataBind();

            var SumMoney = (from a in Query9
                            select a.金额).Sum();
            var SumMount =  (from a in Query9
                            select a.数量).Sum();

            Label_Sum.Text = "当前查询记录的总金额：" + string.Format("{0:N2}", (SumMoney.ToString() == "" ? 0 : SumMoney)) + "元"
                + ";总数量：" + string.Format("{0:N2}", (SumMount.ToString() == "" ? 0 : SumMount));
                             





        }
        #endregion 


        #region       //翻页的功能
        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;

            List<TABC_CW_Meterial> Query1 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query2 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query3 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query4 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query5 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query6 = new List<TABC_CW_Meterial>();
            List<TABC_CW_Meterial> Query7 = new List<TABC_CW_Meterial>();

            string MyShop = TextBox_WorkShop.Text;
            string MyPId = TextBox_ProductID.Text;
            string MyMId = TextBox_MeterialID.Text;
            string MyBId = TextBox_BillNumber.Text;
            string MyCenterName = TextBox_CenterName.Text;

            if (MyShop != "")
            {
                Query1 = (from a in db.TABC_CW_Meterial
                          where a.部门.Contains(MyShop)
                          select a).ToList();
            }
            else
            {
                Query1 = (from a in db.TABC_CW_Meterial
                          select a).ToList();
            }




            if (MyPId != "")
            {
                Query2 = (from a in Query1
                          where a.产品ID.StartsWith(MyPId)
                          select a).ToList();
            }
            else
            {
                Query2 = (from a in Query1
                          select a).ToList();

            }


            if (MyMId != "")
            {
                Query3 = (from a in Query2
                          where a.材料编码.StartsWith(MyMId)
                          select a).ToList();
            }
            else
            {
                Query3 = (from a in Query2
                          select a).ToList();

            }

            if (MyBId != "")
            {
                Query4 = (from a in Query3
                          where (a.领料单号.ToString()).StartsWith(MyBId)
                          select a).ToList();
            }
            else
            {
                Query4 = (from a in Query3
                          select a).ToList();
            }


            if (CheckBox_OnlyErrorRow.Checked == true)
            {
                Query5 = (from a in Query4
                          where a.数据检查 == false
                          select a).ToList();
            }
            else
            {
                Query5 = (from a in Query4
                          where a.数据检查 == true
                          select a).ToList();
            }


            if (CheckBox_Zero.Checked == true)
            {
                Query6 = (from a in Query5
                          where a.金额 == 0
                          select a).ToList();
            }
            else
            {
                Query6 = (from a in Query5
                          select a).ToList();
            }


                if (MyCenterName != "")
                {
                    Query7 = (from a in Query6
                              where a.领用人.Contains(MyCenterName)
                              select a).ToList();
                }
                else
                {
                    Query7 = (from a in Query6
                              select a).ToList();

                }


            MyQuery = Query7.Count();

            return MyQuery;
       
        }

        #endregion



        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            MyDataBind( pageIndx);

            if (GridView_MeterialQuery.Rows.Count == 0)
            { Panel_Main.Visible = false; }
            else
            { Panel_Main.Visible = true; }

        }





        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {
            string myId = e.CommandArgument.ToString();
            var MyEditRows = from m in db.TABC_CW_Meterial
                            where m.ID.ToString() == myId
                            select m;

            if (MyEditRows.Count() == 1)
            {
                var myEditRow = MyEditRows.Single();
            

                Button mybutton = (Button)sender;
                DataControlFieldCell dcf = (DataControlFieldCell)mybutton.Parent;
                GridViewRow gvr = (GridViewRow)dcf.Parent;

                string CenterName = ((TextBox)(gvr.Cells[11].FindControl("TextBox_Center"))).Text;

                var IsValidName = from a in db.TABC_DM_CostCenter_Ref_Username
                                  where a.领用人 == CenterName
                                  select a;
                if(IsValidName.Count() == 1)
                {
                    myEditRow.领用人 = IsValidName.Single().领用人;
                try
                {
                   db.SubmitChanges();

                   alert = "<script>alert('修改成功');</script>";

                }
                catch
                {

                    alert = "<script>alert('修改失败');</script>";
                }
                
                }else
                {
                    alert = "<script>alert('当前领用人不存在，请检查');</script>";
                }


              
            }

            
            


        }
        //点击执行数据检查
        protected void Button_Check_Click(object sender, EventArgs e)
        {

            try
            {
                db.产品原材料检查(Application["accountPeriod"].ToString());
              
                alert = "<script>alert('数据检查完成');</script>";
      
            }
            catch 
            {
                //Response.Write("<script>alert('数据检查失败');</script>");
                alert = "<script>alert('数据检查失败');</script>";
            }
        }

        protected void GridView_MeterialQuery_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }
}
